package com.maruti.otterapi.search;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.maruti.otterapi.Otter4JavaException;
import com.maruti.otterapi.TopsyConfig;
import com.maruti.otterapi.TopsyConstants;
import com.maruti.otterapi.TopsyInvoker;

/**
 * @author Maruti Gollapudi (maruti (dot) gollapudi (at) gmail.com)
 *
 *         Java wrapper for Otter API Version 0.1
 *
 */
public class SearchCount {

	private TopsyConfig topsyConfig;

	public TopsyConfig getTopsyConfig() {
		return topsyConfig;
	}

	public void setTopsyConfig(TopsyConfig topsyConfig) {
		this.topsyConfig = topsyConfig;
	}

	/**
	 * @param query
	 * @return
	 * @throws Otter4JavaException
	 */
	public SearchResponse searchCount(String query) throws Otter4JavaException {
		return search(query, 0, -1, "a", "");
	}

	/**
	 * @param query
	 *            : Refer otter API documentation for query syntax
	 * @param searchTimeLine
	 *            : Time window for results. (Options: dynamic - will pick the
	 *            most relevant window. Possible responses are 1-23 hours or
	 *            1-100 days. (For example: h6 or d10). h last hour, d last day,
	 *            w last week, m last month, a all time. (DEPRECATED: auto -
	 *            automatically pick the most relevant window. This will return
	 *            one of the standard h,d,w,m,a window values.).
	 * @return
	 * @throws Otter4JavaException
	 */
	public SearchResponse search(String query, String searchTimeLine) throws Otter4JavaException {
		return search(query, 0, -1, searchTimeLine, "");
	}

	// Returns list of matches.
	/**
	 * @param query
	 * @param startPage
	 * @param maxNoOfPages
	 * @param searchTimeLine
	 *            : Time window for results. (Options: dynamic - will pick the
	 *            most relevant window. Possible responses are 1-23 hours or
	 *            1-100 days. (For example: h6 or d10). h last hour, d last day,
	 *            w last week, m last month, a all time. (DEPRECATED: auto -
	 *            automatically pick the most relevant window. This will return
	 *            one of the standard h,d,w,m,a window values.).
	 * @param exclude
	 *            : comma seperated list of Strings to exclude from results.
	 * @return
	 * @throws Otter4JavaException
	 */
	public SearchResponse search(String query, int startPage, int maxNoOfPages,
			String searchTimeLine, String exclude) throws Otter4JavaException {
		TopsyInvoker invoker = new TopsyInvoker();
		invoker.setTopsyConfig(topsyConfig);

		List<Post> results = new ArrayList<Post>();

		query = query.replaceAll("\\s", "%20");

		String searchWindowStr = "default";
		SearchResponse data = null;

		data = (SearchResponse) invoker.fetchData(
				TopsyConstants.searchCountBaseURL + query, "search");

		return data;
	}

}
